<?php
class LoginAction extends CommonAction {
    public function index(){
        $this->display();
    }
    
    public function config(){
    	if($this->ispost()){
    		$userModel = M('User');
    		$username = session('username');
    		//data
    		if($this->_post('password')!=null){
    			$data['password'] = sha1($this->_post('password'));
    		}
    		$data['realname'] = $this->_post('realname');
    		$data['sex'] = $this->_post('sex');
    		$data['mobile'] = $this->_post('mobile');
    		$data['phone'] = $this->_post('phone');
    		$data['address'] = $this->_post('address');
    		$data['email'] = $this->_post('email');
    		$data['msnoicq'] = $this->_post('msnoicq');
    		
    		//result
            $result = $userModel->where("username='{$username}'")->save($data);
            if(false !== $result) {
            	$this->json(array('statusCode'=>200,'message'=>'操作成功！'));
            }else{
            	$this->json(array('statusCode'=>300,'message'=>'写入错误！'));
            }
    	}else{
    		$userModel = M("User");
    		$user = $userModel->where(array('username'=>session('username')))->select();
    		$this->assign('user',$user[0]);
    		$this->display();
    	}
    }

    public function checkLogin(){
    	$username = $this->_post('username');
		$password = $this->_post('password');
		$userModel = M("User");
		$user = $userModel->where(array('username'=>$username))->select();

		if(!empty($user) && ($user[0]['password']==sha1($password)) && ($user[0]['userstatus']==2)){
			session('username',$username);
			$this->json(array('statusCode'=>200,'message'=>'登录成功'));
		}else{
			$this->json(array('statusCode'=>300,'message'=>'登录失败,用户或密码错误'));
		}
    }
    
    public function loginout(){
    	session(null);
    	$this->json(array('statusCode'=>200,'message'=>'登出成功'));
    }
}